package com.ttbj.rule.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.ttbj.core.util.StringUtil;
import com.ttbj.rule.dao.BillDisMapper;
import com.ttbj.rule.dao.BillTimingMapper;
import com.ttbj.rule.dao.CarTypeRuleMapper;
import com.ttbj.rule.po.BillDis;
import com.ttbj.rule.po.BillTiming;
import com.ttbj.rule.po.CarTypeRule;
import com.ttbj.rule.service.WtBillService;
import com.ttbj.util.Assert;
import com.ttbj.util.ResultUtil;
import com.zywl.common.utils.DateUtils;

@Service
public class WtBillServiceImpl implements WtBillService {

	@Autowired
	private BillTimingMapper billTimingMapper = null;

	@Autowired
	private CarTypeRuleMapper carTypeRuleMapper = null;
	
	@Autowired
	private BillDisMapper billDisMapper = null;
	
	

	@Override
	public Float getWtCarType(String carType) throws Exception {
		if (!StringUtil.isNumeric(carType)) {
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 421));
		}
		Long typeId = Long.valueOf(carType);
		CarTypeRule rule = carTypeRuleMapper.selectById(typeId);
		Assert.assertNonNullField(rule, 422, "typeWt");
		return rule.getTypeWt();
	}

	@Override
	public Float getWtTiming(int timingId) throws Exception {
		BillTiming rule = billTimingMapper.selectByPrimaryKey(timingId);
		Assert.assertNonNullField(rule, "昼夜权重计费规则获取失败",900,null,"timingDayStart","timingNightStart","timingNightRate","timingDayRate");
		int hour = DateUtils.getHour();
		Integer day = rule.getTimingDayStart();
		Integer night = rule.getTimingNightStart();
		if (hour >= night || hour < day) {
			return rule.getTimingNightRate();//夜
		}
		return rule.getTimingDayRate();//昼
	}

	@Override
	public BillDis getRuleDistance() throws Exception {
		BillDis billDis = billDisMapper.selectByNewest();
		Assert.assertNonNullField(billDis, "里程计费规则获取失败",900,null,"distanceStartPrice","distanceStartMileage","distanceBeyondPrice","distanceLong");
		return billDis;
	}

}
